package dataAccessLayer.tests;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.Test;

import org.eclipse.hyades.test.common.junit.DefaultTestArbiter;
import org.eclipse.hyades.test.common.junit.HyadesTestCase;
import org.eclipse.hyades.test.common.junit.HyadesTestSuite;

import java.sql.PreparedStatement;
import java.util.ArrayList;

import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.factoryMySQL.daos.DAOcitiesMySQL;
import dataAccessLayer.visualObject.VOcity;

/**
 * Generated code for the test suite <b>testDAOcities</b> located at
 * <i>/Pastry/src/dataAccessLayer/tests/testDAOcities.testsuite</i>.
 */
public class testDAOcities extends HyadesTestCase {
	
	DBManager manager=new DBManager("local");
	DAOcitiesMySQL DAOcities=new DAOcitiesMySQL(manager);
	/**
	 * Constructor for testDAOcities.
	 * @param name
	 */
	public testDAOcities(String name) {
		super(name);
	}

	/**
	 * Returns the JUnit test suite that implements the <b>testDAOcities</b> definition.
	 */
	public static Test suite() {
		HyadesTestSuite testDAOcities = new HyadesTestSuite("testDAOcities");
		testDAOcities.setArbiter(DefaultTestArbiter.INSTANCE).setId(
				"A1DD966B662D1300ED8EB53731343731");
		testDAOcities.addTest(new testDAOcities("testinsertCity").setId(
				"A1DD966B6A082A50ED8EB53731343731").setTestInvocationId(
				"A1DD96707F4450B0DC87E13162636333"));
		testDAOcities.addTest(new testDAOcities("testupdateCity").setId(
				"A1DD966B6DA54E40ED8EB53731343731").setTestInvocationId(
				"A1DD96707F54F280DC87E13162636333"));
		testDAOcities.addTest(new testDAOcities("testremoveCity").setId(
				"A1DD966B70518280ED8EB53731343731").setTestInvocationId(
				"A1DD96707F59AD70DC87E13162636333"));
		testDAOcities.addTest(new testDAOcities("testlistCities").setId(
				"A1DD966B74A88090ED8EB53731343731").setTestInvocationId(
				"A1DD96707F5E6860DC87E13162636333"));
		testDAOcities.addTest(new testDAOcities("testsearchCityByCode").setId(
				"A1DD966B78A29190ED8EB53731343731").setTestInvocationId(
				"A1DD96707F634A60DC87E13162636333"));
		return testDAOcities;
	}

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
	}

	/**
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
	}

	/**
	 * testinsertCity
	 * Probar la insercion de una ciudad.
	 * @throws Exception
	 */
	public void testinsertCity() throws Exception
	{
		VOcity vOPCity= new VOcity();
		vOPCity.setName("Bogota");
		DAOcities.insertCity(vOPCity);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String nombre="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM cities ORDER BY code DESC LIMIT 1");
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				nombre=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOPCity.getName(), nombre);
	}

	/**
	 * testupdateCity
	 * Probar la actualizacion.
	 * @throws Exception
	 */
	public void testupdateCity() throws Exception 
	{
		VOcity vOPCity= new VOcity();
		vOPCity.setCode(13);
		vOPCity.setName("SantaFeBogota");
		DAOcities.updateCity(vOPCity);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String nombre="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM cities where code=?");
			preparedStatement.setInt(1,vOPCity.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				nombre=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOPCity.getName(), nombre);
	}

	/**
	 * testremoveCity
	 * Probar eliminar una ciudad.
	 * @throws Exception
	 */
	public void testremoveCity() throws Exception
	{
		VOcity vOPCity= new VOcity();
		vOPCity.setCode(11);
		
		DAOcities.removeCity(vOPCity);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		VOcity searchVoCity = new VOcity();
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM cities where code=?");
			preparedStatement.setInt(1,vOPCity.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				searchVoCity.setCode(resultSet.getInt(1));
				searchVoCity.setName(resultSet.getString(2));
			}
			else
			{
				searchVoCity=null;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		assertEquals(searchVoCity,null );
	}

	/**
	 * testlistCities
	 * Probar la consulta de todas las ciudades.
	 * @throws Exception
	 */
	public void testlistCities() throws Exception 
	{
		
		ArrayList<VOcity> search=DAOcities.listCities();
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		
		int count=0;
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM cities");
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				count++;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(search.size(),count );
	}

	/**
	 * testsearchCityByCode
	 * Buscar por un codigo la pelicula
	 * @throws Exception
	 */
	public void testsearchCityByCode() throws Exception 
	{
		
		VOcity vOPCity= new VOcity();
		vOPCity.setCode(1);
		
		VOcity search=DAOcities.searchCityByCode(vOPCity);
		
		
		assertEquals(vOPCity.getCode(),search.getCode());
	}

}
